package com.javabasic.algorithm.sword_of_offer;

/**
 * @author xiongmin
 * @version 0.0.1
 * @description Created by work on 2021/10/14 11:52 下午
 * @see
 */
public class 可以被一步捕获的棋子数 {
    public int numRookCaptures(char[][] board) {
        // 约定是8*8的二维数组
        int len = 8;
        int raw = 0, col = 0;
        boolean finish = false;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                if (board[i][j] == 'R') {
                    raw = i;
                    col = j;
                    finish = true;
                    break;
                }
            }
            if (finish) {
                break;
            }
        }

        /**
         * 从目标节点的上下左右4个方向扩展
         */
        int result = 0;
        //  上方向
        for (int i = raw-1; i >= 0; i--) {
            if (board[i][col] != '.') {
                if (board[i][col] == 'p') {
                    result++;
                }
                break;
            }
        }
        // 下方向
        for (int i = raw+1; i < len; i++) {
            if (board[i][col] != '.') {
                if (board[i][col] == 'p') {
                    result++;
                }
                break;
            }
        }
        // 左方向
        for (int i = col-1; i >= 0; i--) {
            if (board[raw][i] != '.') {
                if (board[raw][i] == 'p') {
                    result++;
                }
                break;
            }
        }
        // 右方向
        for (int i = col+1; i < len; i++) {
            if (board[raw][i] != '.') {
                if (board[raw][i] == 'p') {
                    result++;
                }
                break;
            }
        }
        return result;
    }
}
